package com.xdkj.web.servlet;

import com.xdkj.commons.util.BaseResponse;
import com.xdkj.commons.util.ResponseEnum;
import com.xdkj.model.dao.TEmployeeDao;
import com.xdkj.model.dao.impl.TEmployeeDaoImpl;
import com.xdkj.model.entity.TEmployee;
import com.xdkj.model.vo.TEmployeeVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "TEmployeeServlet", urlPatterns = "/employee")
public class TEmployeeServlet extends BaseServlet {
    private static final Logger log = LoggerFactory.getLogger(TEmployeeServlet.class);
    private TEmployeeDao employeeDao = new TEmployeeDaoImpl();

    /**
     * 分页查询
     *
     * @param request  请求
     * @param response 响应
     * @return {@link BaseResponse}
     * @throws ServletException servlet异常
     * @throws IOException      ioexception
     */
    public BaseResponse queryByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        //查询所有
        List<TEmployeeVo> tEmployeeVos = employeeDao.queryAll();
        List<TEmployeeVo> pageList = employeeDao.queryByPage(Integer.parseInt(page), Integer.parseInt(limit));
        BaseResponse baseResponse = null;
        if (pageList.size() > 0) {
            baseResponse = BaseResponse.ok(0, ResponseEnum.QUERYSUCCESS.getMessage(), pageList);
            baseResponse.setCount(tEmployeeVos.size());
        } else {
            baseResponse = BaseResponse.error(ResponseEnum.QUERYFAILURE.getCode(), ResponseEnum.QUERYFAILURE.getMessage());
        }
        return baseResponse;
    }

    /**
     * 通过id删除
     *
     * @param request  请求
     * @param response 响应
     * @return {@link BaseResponse}
     * @throws ServletException servlet异常
     * @throws IOException      ioexception
     */
    public BaseResponse deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        //查询所有
        boolean result = employeeDao.deleteById(Integer.parseInt(id));
        BaseResponse baseResponse = null;
        if (result) {
            baseResponse = BaseResponse.ok(ResponseEnum.DELETESUCCESS.getCode(), ResponseEnum.DELETESUCCESS.getMessage());
        } else {
            baseResponse = BaseResponse.error(ResponseEnum.DELETEFAILURE.getCode(), ResponseEnum.DELETEFAILURE.getMessage());
        }
        return baseResponse;
    }

    /**
     * 做更新员工
     *
     * @param request  请求
     * @param response 响应
     * @return {@link BaseResponse}
     * @throws Exception 异常
     */
    public BaseResponse doUpdateEmployee(HttpServletRequest request, HttpServletResponse response) throws Exception {
        log.info("修改员工数据");
        //接受参数
        TEmployee employee = new TEmployee();
        //注册日期转换器
        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class aClass, Object o) {
                // 如果字符串为null，则返回null
                if (o == null)
                    return null;

                // 如果字符串为空格，则返回null
                String str = (String) o;
                if (str.trim().equals(""))
                    return null;

                // 将字符串转为Date类型
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return formatter.parse(o.toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return new Date();
            }
        }, Date.class);
        BeanUtils.populate(employee, request.getParameterMap());
        log.info("请求中获取到的员工数据{}", employee);
        boolean result = employeeDao.updateEmployee(employee);
        BaseResponse baseResponse = null;
        if (result) {
            baseResponse = BaseResponse.ok(ResponseEnum.UPDATESUCCESS.getCode(), ResponseEnum.UPDATESUCCESS.getMessage());
        } else {
            baseResponse = BaseResponse.error(ResponseEnum.UPDATEFAILURE.getCode(), ResponseEnum.UPDATEFAILURE.getMessage());

        }
        return baseResponse;
    }
}
